#coding=utf-8
#Boa:Dialog:DlgCustomerEditor

import wx
import wx.lib.buttons
from product import *
from config import *
import kit
from ctrlAider import MyListCtrlDecorator

def create(parent):
    return DlgCustomerEditor(parent)

[wxID_DLGCUSTOMEREDITOR, wxID_DLGCUSTOMEREDITORBTNADDCONTACT, 
 wxID_DLGCUSTOMEREDITORBTNCANCEL, wxID_DLGCUSTOMEREDITORBTNDELCONTACT, 
 wxID_DLGCUSTOMEREDITORBTNKEYWORD, wxID_DLGCUSTOMEREDITORBTNREGION, 
 wxID_DLGCUSTOMEREDITORBTNSAVE, wxID_DLGCUSTOMEREDITORBTNTYPE, 
 wxID_DLGCUSTOMEREDITORCBCUSTTYPE, wxID_DLGCUSTOMEREDITORLCCONTACT, 
 wxID_DLGCUSTOMEREDITORSTATICTEXT1, wxID_DLGCUSTOMEREDITORTADDR, 
 wxID_DLGCUSTOMEREDITORTBRIEFCODE, wxID_DLGCUSTOMEREDITORTCADDR, 
 wxID_DLGCUSTOMEREDITORTCBRIEFCODE, wxID_DLGCUSTOMEREDITORTCDEBT, 
 wxID_DLGCUSTOMEREDITORTCEMAIL, wxID_DLGCUSTOMEREDITORTCFAX, 
 wxID_DLGCUSTOMEREDITORTCMEMO, wxID_DLGCUSTOMEREDITORTCNAME, 
 wxID_DLGCUSTOMEREDITORTCONTACT, wxID_DLGCUSTOMEREDITORTCPHONE, 
 wxID_DLGCUSTOMEREDITORTCPRINTNAME, wxID_DLGCUSTOMEREDITORTCREGION, 
 wxID_DLGCUSTOMEREDITORTDEBT, wxID_DLGCUSTOMEREDITORTDEBTMEMO, 
 wxID_DLGCUSTOMEREDITORTEMAIL, wxID_DLGCUSTOMEREDITORTFAX, 
 wxID_DLGCUSTOMEREDITORTKEYWORD, wxID_DLGCUSTOMEREDITORTMEMO, 
 wxID_DLGCUSTOMEREDITORTNAME, wxID_DLGCUSTOMEREDITORTPHONE, 
 wxID_DLGCUSTOMEREDITORTPRINTNAME, wxID_DLGCUSTOMEREDITORTREGION, 
 wxID_DLGCUSTOMEREDITORTTYPE, 
] = [wx.NewId() for _init_ctrls in range(35)]

class DlgCustomerEditor(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGCUSTOMEREDITOR,
              name=u'DlgCustomerEditor', parent=prnt, pos=wx.Point(454, 102),
              size=wx.Size(720, 668), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u5ba2\u6237\u7f16\u8f91')
        self.SetClientSize(wx.Size(712, 634))
        self.Center(wx.BOTH)
        self.SetBackgroundColour(wx.Colour(255, 255, 255))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False, u'Arial'))

        self.TName = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTNAME,
              label=u'\u5ba2\u6237\u540d\u79f0\uff1a', name=u'TName',
              parent=self, pos=wx.Point(24, 80), size=wx.Size(88, 24), style=0)

        self.TCName = wx.TextCtrl(id=wxID_DLGCUSTOMEREDITORTCNAME,
              name=u'TCName', parent=self, pos=wx.Point(120, 80),
              size=wx.Size(144, 28), style=0, value=u'')
        self.TCName.Bind(wx.EVT_TEXT, self.OnTCNameText,
              id=wxID_DLGCUSTOMEREDITORTCNAME)

        self.TBriefCode = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTBRIEFCODE,
              label=u'\u52a9\u8bb0\u7801\uff1a', name=u'TBriefCode',
              parent=self, pos=wx.Point(392, 80), size=wx.Size(68, 19),
              style=0)

        self.TCBriefCode = wx.TextCtrl(id=wxID_DLGCUSTOMEREDITORTCBRIEFCODE,
              name=u'TCBriefCode', parent=self, pos=wx.Point(504, 80),
              size=wx.Size(136, 28), style=0, value=u'')

        self.TAddr = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTADDR,
              label=u'\u8054\u7cfb\u5730\u5740\uff1a', name=u'TAddr',
              parent=self, pos=wx.Point(24, 240), size=wx.Size(88, 16),
              style=0)

        self.TEmail = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTEMAIL,
              label=u'\u7535\u5b50\u90ae\u4ef6\uff1a', name=u'TEmail',
              parent=self, pos=wx.Point(392, 160), size=wx.Size(96, 24),
              style=0)

        self.TCAddr = wx.TextCtrl(id=wxID_DLGCUSTOMEREDITORTCADDR,
              name=u'TCAddr', parent=self, pos=wx.Point(120, 240),
              size=wx.Size(520, 28), style=0, value=u'')

        self.TCEmail = wx.TextCtrl(id=wxID_DLGCUSTOMEREDITORTCEMAIL,
              name=u'TCEmail', parent=self, pos=wx.Point(504, 160),
              size=wx.Size(136, 28), style=0, value=u'')
        self.TCEmail.SetAutoLayout(False)

        self.TFax = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTFAX,
              label=u'\u4f20      \u771f\uff1a', name=u'TFax', parent=self,
              pos=wx.Point(392, 200), size=wx.Size(88, 24), style=0)

        self.TCFax = wx.TextCtrl(id=wxID_DLGCUSTOMEREDITORTCFAX, name=u'TCFax',
              parent=self, pos=wx.Point(504, 200), size=wx.Size(136, 28),
              style=0, value=u'')

        self.TMemo = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTMEMO,
              label=u'\u5907      \u6ce8\uff1a', name=u'TMemo', parent=self,
              pos=wx.Point(24, 432), size=wx.Size(80, 24), style=0)

        self.TCMemo = wx.TextCtrl(id=wxID_DLGCUSTOMEREDITORTCMEMO,
              name=u'TCMemo', parent=self, pos=wx.Point(120, 432),
              size=wx.Size(512, 64), style=wx.TE_MULTILINE, value=u'')

        self.TDebt = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTDEBT,
              label=u'\u671f\u521d\u5e94\u6536\uff1a', name=u'TDebt',
              parent=self, pos=wx.Point(24, 512), size=wx.Size(88, 24),
              style=0)

        self.TCDebt = wx.TextCtrl(id=wxID_DLGCUSTOMEREDITORTCDEBT,
              name=u'TCDebt', parent=self, pos=wx.Point(120, 504),
              size=wx.Size(100, 28), style=0, value=u'0')

        self.TDebtMemo = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTDEBTMEMO,
              label=u'\uff08\u65b0\u589e\u5ba2\u6237\u65f6\u6b20\u6b3e\u91d1\u989d\uff0c\u4e0e\u8ba2\u5355\u6b20\u6b3e\u7d2f\u52a0\uff09',
              name=u'TDebtMemo', parent=self, pos=wx.Point(224, 512),
              size=wx.Size(344, 24), style=0)

        self.btnSave = wx.Button(id=wxID_DLGCUSTOMEREDITORBTNSAVE,
              label=u'\u4fdd\u5b58', name=u'btnSave', parent=self,
              pos=wx.Point(248, 576), size=wx.Size(80, 32), style=0)
        self.btnSave.SetDefault()
        self.btnSave.Bind(wx.EVT_BUTTON, self.OnBtnSaveButton,
              id=wxID_DLGCUSTOMEREDITORBTNSAVE)

        self.btnCancel = wx.Button(id=wxID_DLGCUSTOMEREDITORBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(448, 576), size=wx.Size(80, 32), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGCUSTOMEREDITORBTNCANCEL)

        self.tRegion = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTREGION,
              label=u'\u6240\u5c5e\u5730\u533a\uff1a', name=u'tRegion',
              parent=self, pos=wx.Point(24, 200), size=wx.Size(85, 19),
              style=0)

        self.tType = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTTYPE,
              label=u'\u5ba2\u6237\u7c7b\u578b\uff1a', name=u'tType',
              parent=self, pos=wx.Point(24, 160), size=wx.Size(85, 19),
              style=0)

        self.tKeyword = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTKEYWORD,
              label=u'\u5173\u952e\u5b57\uff1a', name=u'tKeyword', parent=self,
              pos=wx.Point(24, 280), size=wx.Size(68, 19), style=0)

        self.btnKeyword = wx.lib.buttons.GenButton(id=wxID_DLGCUSTOMEREDITORBTNKEYWORD,
              label=u'\u5ba2\u6237\u5173\u952e\u5b57\u6807\u7b7e\u9009\u62e9',
              name=u'btnKeyword', parent=self, pos=wx.Point(120, 280),
              size=wx.Size(520, 31), style=wx.SIMPLE_BORDER)
        self.btnKeyword.SetAutoLayout(False)
        self.btnKeyword.Bind(wx.EVT_BUTTON, self.OnGenBtnKeywordButton,
              id=wxID_DLGCUSTOMEREDITORBTNKEYWORD)

        self.TCRegion = wx.TextCtrl(id=wxID_DLGCUSTOMEREDITORTCREGION,
              name=u'TCRegion', parent=self, pos=wx.Point(120, 200),
              size=wx.Size(112, 28), style=0, value=u'')

        self.btnRegion = wx.lib.buttons.GenButton(id=wxID_DLGCUSTOMEREDITORBTNREGION,
              label=u'+', name=u'btnRegion', parent=self, pos=wx.Point(232,
              200), size=wx.Size(32, 28), style=0)
        self.btnRegion.Bind(wx.EVT_BUTTON, self.OnBtnRegionButton,
              id=wxID_DLGCUSTOMEREDITORBTNREGION)

        self.btnType = wx.lib.buttons.GenButton(id=wxID_DLGCUSTOMEREDITORBTNTYPE,
              label=u'+', name=u'btnType', parent=self, pos=wx.Point(232, 160),
              size=wx.Size(32, 28), style=0)
        self.btnType.Bind(wx.EVT_BUTTON, self.OnBtnTypeButton,
              id=wxID_DLGCUSTOMEREDITORBTNTYPE)

        self.tContact = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTCONTACT,
              label=u'\u8054\u7cfb\u4eba\uff1a', name=u'tContact', parent=self,
              pos=wx.Point(24, 320), size=wx.Size(68, 19), style=0)

        self.lcContact = wx.ListCtrl(id=wxID_DLGCUSTOMEREDITORLCCONTACT,
              name=u'lcContact', parent=self, pos=wx.Point(120, 320),
              size=wx.Size(456, 104), style=wx.LC_REPORT)

        self.btnAddContact = wx.Button(id=wxID_DLGCUSTOMEREDITORBTNADDCONTACT,
              label=u'\u65b0\u589e', name=u'btnAddContact', parent=self,
              pos=wx.Point(584, 320), size=wx.Size(56, 29), style=0)
        self.btnAddContact.Enable(False)
        self.btnAddContact.Bind(wx.EVT_BUTTON, self.OnBtnAddContactButton,
              id=wxID_DLGCUSTOMEREDITORBTNADDCONTACT)

        self.btnDelContact = wx.Button(id=wxID_DLGCUSTOMEREDITORBTNDELCONTACT,
              label=u'\u5220\u9664', name=u'btnDelContact', parent=self,
              pos=wx.Point(584, 360), size=wx.Size(56, 29), style=0)
        self.btnDelContact.Enable(False)
        self.btnDelContact.Bind(wx.EVT_BUTTON, self.OnBtnDelContactButton,
              id=wxID_DLGCUSTOMEREDITORBTNDELCONTACT)

        self.tPrintName = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTPRINTNAME,
              label=u'\u6253\u5370\u540d\u79f0\uff1a', name=u'tPrintName',
              parent=self, pos=wx.Point(24, 120), size=wx.Size(85, 19),
              style=0)

        self.TCPrintName = wx.TextCtrl(id=wxID_DLGCUSTOMEREDITORTCPRINTNAME,
              name=u'TCPrintName', parent=self, pos=wx.Point(120, 120),
              size=wx.Size(144, 28), style=0, value=u'')

        self.staticText1 = wx.StaticText(id=wxID_DLGCUSTOMEREDITORSTATICTEXT1,
              label=u'\u7f16\u8f91\u5ba2\u6237', name='staticText1',
              parent=self, pos=wx.Point(128, 24), size=wx.Size(84, 22),
              style=0)
        self.staticText1.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD,
              False, u'Arial'))
        self.staticText1.SetForegroundColour(wx.Colour(255, 0, 0))

        self.TPhone = wx.StaticText(id=wxID_DLGCUSTOMEREDITORTPHONE,
              label=u'\u5355\u4f4d\u7535\u8bdd\uff1a', name=u'TPhone',
              parent=self, pos=wx.Point(392, 120), size=wx.Size(85, 19),
              style=0)

        self.TCPhone = wx.TextCtrl(id=wxID_DLGCUSTOMEREDITORTCPHONE,
              name=u'TCPhone', parent=self, pos=wx.Point(504, 120),
              size=wx.Size(136, 28), style=0, value=u'')

        self.cbCustType = wx.ComboBox(choices=[],
              id=wxID_DLGCUSTOMEREDITORCBCUSTTYPE, name=u'cbCustType',
              parent=self, pos=wx.Point(120, 160), size=wx.Size(112, 27),
              style=0, value=u'')
        self.cbCustType.SetLabel(u'')

    def __init__(self, parent):
        self._init_ctrls(parent)

        self.myInit()
        
    def myInit(self):
        self.curObj = None
        
        self.regionObj = CDataCustomerRegion.getDetault()
        self.type = ''
        self.curKeywords = []
        self.keywordChanged = False
        
        self.contactChanged = False
        
        cols = [(CDataCustomerContact.iId, wx.LIST_FORMAT_LEFT, 0),
                (CDataCustomerContact.iName, wx.LIST_FORMAT_LEFT, 100),
                (CDataCustomerContact.iPhone, wx.LIST_FORMAT_LEFT, 150),
                (CDataCustomerContact.iMemo, wx.LIST_FORMAT_LEFT, 200)]
                
        self.listCtrl  = MyListCtrlDecorator(self.lcContact, cols, CDataCustomerContact, False)
    
        self.__init_cust_type()
        
    def __init_cust_type(self):   
        self.cbCustType.Clear()
        for id,name in CDataCustomerType.fetchData(fields=[CDataCustomerType.fi_id, 
                                            CDataCustomerType.fi_name]):
            self.cbCustType.Append(name, id)
        
    def setDataObj(self, custObj):  
        self.curObj = custObj
        
        self.TCName.SetValue(self.curObj.getFieldVal(index=CDataCustomer.COL_NAME))
        self.TCPrintName.SetValue(self.curObj.getValue(CDataCustomer.COL_PRINTNAME))
        self.TCBriefCode.SetValue(self.curObj.getFieldVal(index=CDataCustomer.COL_BREIFCODE))
        #self.TCLinkman.SetValue(self.curObj.getFieldVal(index=CDataCustomer.COL_LINKMAN))
        self.TCEmail.SetValue(self.curObj.getFieldVal(index=CDataCustomer.COL_EMAIL))
        self.TCAddr.SetValue(self.curObj.getFieldVal(index=CDataCustomer.COL_ADDR))
        self.TCFax.SetValue(self.curObj.getFieldVal(index=CDataCustomer.COL_FAX))
        self.TCPhone.SetValue(self.curObj.getFieldVal(index=CDataCustomer.COL_LMPHONE))
        self.TCMemo.SetValue(self.curObj.getFieldVal(index=CDataCustomer.COL_MEMO))
        self.TCDebt.SetValue(str(self.curObj.getFieldVal(index=CDataCustomer.COL_DEBT)))
        
        # 141227 - 增加类型、地区、关键字
        custType = CDataCustomerType.findObj(self.curObj.getFieldVal(index=CDataCustomer.COL_TYPE))
                                    
        self.TCType.SetValue(custType.getValue(CDataCustomerType.fi_name))
        
        regionId = self.curObj.getFieldVal(index=CDataCustomer.COL_REGION)
        self.regionObj = CDataCustomerRegion.getFirstInstance({CDataCustomerRegion.fi_id:regionId})
        self.TCRegion.SetValue(self.regionObj.getValue(self.regionObj.fi_name))
        
        self.curKeywords = CDataCustomerLabeling.getKeyword(custObj.getValue(custObj.COL_ID))
        self.btnKeyword.SetLabel(kit.concat(self.curKeywords, ','))
        
        # 141227 - 增加联系人编辑
        self.listCtrl.setFilter({CDataCustomerContact.iPeerId:custObj.getValue(custObj.COL_ID)})
        self.listCtrl.refresh()
        self.btnAddContact.Enable(True)
        self.btnDelContact.Enable(True)
        
    def saveCustLabel(self):
        custId = self.curObj.getValue(self.curObj.COL_ID)
        CDataCustomerLabeling.delData({CDataCustomerLabeling.fi_custId:custId})
        
        for i in range(len(self.curKeywords)):
            labelObj = CDataCustomerLabel.getFirstInstance({CDataCustomerLabel.fi_name:self.curKeywords[i]})
            
            obj = CDataCustomerLabeling.factory()
            fields = {CDataCustomerLabeling.fi_custId:custId,
                        CDataCustomerLabeling.fi_labelId:labelObj.getValue(labelObj.fi_id)}
            obj.setMulFieldVal(fields)
            obj.insert()
            
    def OnBtnSaveButton(self, event):     
##        data = {u'name':self.TCName.GetValue(),
##                u'briefCode':self.TCBriefCode.GetValue(),
##                u'addr':self.TCAddr.GetValue(),
##                u'email':self.TCEmail.GetValue(),
##                u'phone':self.TCPhone.GetValue(),
##                u'fax':self.TCFax.GetValue(),
##                u'memo':self.TCMemo.GetValue(),
##                u'linkman':self.TCLinkman.GetValue(),
##                u'debt':self.TCDebt.GetValue()}
##                
##        cust = CDataCustomer()
##        for (field,val) in data.items():
##            n = CDataCustomer.getFieldNo(field)
##            cust.m_fieldVals[n] = val
##        cust.insertNewData()

        if (self.TCName.GetValue() == ''):
            dlg = wx.MessageDialog(self, u'必须设置客户姓名', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        #custType = CDataCustomerType.getFirstInstance({CDataCustomerType.fi_name:self.TCType.GetValue()})

        custType = self.cbCustType.GetClientData(self.cbCustType.GetSelection()) 
             
        data = {CDataCustomer.COL_NAME:self.TCName.GetValue(),
                CDataCustomer.COL_PRINTNAME:self.TCPrintName.GetValue(),
                CDataCustomer.COL_BREIFCODE:self.TCBriefCode.GetValue(),
                CDataCustomer.COL_ADDR:self.TCAddr.GetValue(),
                CDataCustomer.COL_EMAIL:self.TCEmail.GetValue(),
                CDataCustomer.COL_LMPHONE:self.TCPhone.GetValue(),
                CDataCustomer.COL_FAX:self.TCFax.GetValue(),
                CDataCustomer.COL_MEMO:self.TCMemo.GetValue(),
                #CDataCustomer.COL_LINKMAN:self.TCLinkman.GetValue(),
                CDataCustomer.COL_DEBT:float(self.TCDebt.GetValue()),
                CDataCustomer.COL_REGION:self.regionObj.getValue(self.regionObj.fi_id),
                CDataCustomer.COL_TYPE:custType,}
        
        if (self.curObj is not None):
            self.curObj.setMulFieldVal(data)
            self.curObj.update()
        else:
            obj = CDataCustomer.factory()
            #初始化时分配新id，需注意不能同时初始化多个同类对象会造成id冲突
            #更重要的是取消autoId，因为用了autoId，设置id就没用了
            #data[CDataCustomer.COL_ID] = CDataCustomer.newAutoIncrId()
            # 140711
            #obj.assignNewId()
            obj.setMulFieldVal(data)
            obj.insert()
            self.curObj = obj
        
        # 141227 - 增加类型、地区、关键字
        if self.keywordChanged:
            self.saveCustLabel()
               
        #self.SetReturnCode(wx.ID_OK)
        self.EndModal(wx.ID_OK)
        
    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)

    def OnTCNameText(self, event):
        name = self.TCName.GetValue()
        code = CBriefCode.getBriefCode(name)
        self.TCBriefCode.SetValue(code)

    def OnGenBtnKeywordButton(self, event):
        import DlgCustomerLabel
        
##        btn = event.GetEventObject()
##        btnPos = btn.ClientToScreen( (0,0) )
##        btnSize =  btn.GetSize()
##        
##        dlg = DlgCustomerLabel.create(self)
##        #dlg.setData(self.curKeywords)
##        #dlgSize = dlg.GetSize()
##        dlg.Move((btnPos.x, btnPos.y+btnSize[1]))
        dlg = DlgCustomerLabel.create(self)
        dlg.setEditData(self.curKeywords)
        if (dlg.ShowModal() == wx.ID_OK):
            self.keywordChanged = True
            self.curKeywords = dlg.getSelected()
            self.btnKeyword.SetLabel(kit.concat(self.curKeywords, ','))

    def OnBtnRegionButton(self, event):
        import DlgCustRegion
        
        dlg = DlgCustRegion.create(self)
        if (dlg.ShowModal() == wx.ID_OK):
            self.regionObj = dlg.getSelectObj()
            self.TCRegion.SetValue(self.regionObj.getValue(self.regionObj.fi_name))

    def OnBtnTypeButton(self, event):
        import DlgCustType
        
        dlg = DlgCustType.create(self)
        if (dlg.ShowModal() == wx.ID_OK):
            self.__init_cust_type()
            self.cbCustType.SetValue(dlg.getSelectStr())

    def getSelectPeer(self):
        return self.curObj
    
    def OnBtnAddContactButton(self, event):
        import DlgEditorContact
        dlg = DlgEditorContact.create(self)
        if (dlg.ShowModal() == wx.ID_OK):
            
            obj = dlg.getContactObj()
            self.listCtrl.append(obj)                
            
            #self.contactChanged = True

    def OnBtnDelContactButton(self, event):
        item = self.lcContact.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            dlg = wx.MessageDialog(self, u'请先选择要删除的项目',u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        id = int(self.lcContact.GetItemText(item))
        obj = CDataCustomerContact.getFirstInstance({CDataCustomerContact.iId:id})
        obj.delete()
        self.lcContact.DeleteItem(item)
        #self.contactChanged = True
